// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import {
  store
} from './store/store.js'
import axios from 'axios'
// 解决IE的不兼容promise对象问题
import promise from 'es6-promise'
promise.polyfill()
Vue.prototype.$ajax = axios
// base64解码
Vue.prototype.b64DecodeUnicode = function (str) {
  // return decodeURIComponent(escape(atob(str)))
  return str
}
Vue.prototype.numLen = function (str) {
  if (str.length >= 6) {
    var res = str.substr(0, str.length - 4) + "万"
    return res
  } else {
    return str
  }
}
// IP
// Vue.prototype.Ipurl = "http://10.0.1.129:20050/"
// server
// Vue.prototype.Ipurl = "http://47.106.248.34:20050/"
Vue.prototype.Ipurl = "http://x-newmedia.com:20050/"
// 全局收藏夹数量
Vue.prototype.ctrlNum = 0
Vue.prototype.desc = 0
// 用户信息
Vue.prototype.userInfo = {
  mobile: "",
  token: "",
  userId: ""
}
Vue.prototype.connectQQ = function () {
  window.open("http://q.url.cn/abManH?_type=wpa&qidian=true")
}
Vue.use(ElementUI)


// 请求拦截器
axios.interceptors.request.use(config => {
  axios.prototype.$loging = Vue.prototype.$loading({
    lock: true,
    text: '美辰君为您查找数据...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})
// 响应拦截器
axios.interceptors.response.use((response) => {
  axios.prototype.$loging.close()
  if (!response || response.status !== 200) return
  switch (response.data.returnCode * 1) {
    case 1000:
    case 1002: // 请求成功，但列表数据为空
    case 2009:
      return response
    case 1004:
      break
    case 2000:
      break
    case 1009:
      break
    default:
      return response
  }
  // Message({
  //   message: response.data.message,
  //   type: 'error',
  //   duration: 3 * 1000
  // })
  return response
}, (error) => {
  axios.prototype.$loging.close()
  Message({
    message: error.message,
    type: 'error',
    duration: 3 * 1000
  })
  return Promise.reject(error)
})
// 路由钩子实现定向头部
Vue.config.productionTip = false
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  template: '<App/>',
  store
})
